home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 February: Tool Chest / Apple Developer CD Series Tool Chest February 1996 (Apple Computer)(1996).iso / Sample Code / Snippets / Testing & Debugging / Audit / Src / DisplayAuditFile.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-07-21  |  4.3 KB  |  167 lines  |  [TEXT/KAHL]

  1. /*                                DisplayAuditFile.c                                */
  2. /*
  3.  * DisplayAuditFile.c
  4.  * Copyright © 1992-93, Apple Computer Inc. All Rights Reserved.
  5.  * Output file writer for the Audit library.
  6.  * Edit History
  7.  *    93.01.14 MM        Removed Think C stdio dependencies. Added  Standard Fil
  8.  *                    dialog with additional dialog items to allow defining the
  9.  *                    audit selector.
  10.  *    93.01.19 MM        MPW \r is the wrong character. Force <returns> on both MPW and
  11.  *                    Think, because this is what the Mac file system requires for
  12.  *                    end of line. (Change is in DisplayAuditFile.c)
  13.  *    93.07.09 MM        Reformatted. CloseOutputFile now takes file refNum, volume
  14.  *                    refNum and filename parameters, rather than a SFReply.
  15.  */
  16. #include "DisplayAudit.h"
  17. #define kEndOfLine                0x0D    /* Return            */
  18.  
  19.  
  20. /*
  21.  * File Output
  22.  * Open/create a file to write the audit information. reply.good is TRUE on
  23.  * success, FALSE if the user cancels the SFPutFile. Serious errors fail via the
  24.  * failure mechanism. The file is TEXT type. The creator is TeachText. On success:
  25.  *         reply.vRefNum        The volume id.
  26.  *         fileRefNum            The file id.
  27.  */
  28. void
  29. PromptAndCreateAuditOutputFile(
  30.         ConstStr255Param                promptString,
  31.         ConstStr255Param                defaultFileName,
  32.         OSType                            creator,
  33.         short                            *fileRefNum,
  34.         short                            *volumeRefNum,
  35.         Str63                            fileName
  36.     )
  37. {
  38.         Point                            where;
  39.         DialogTHndl                        dialog;
  40.         Rect                            box;
  41.         SFReply                            reply;
  42.         
  43.         /*
  44.          * Center the dialog
  45.          */
  46.         dialog = (DialogTHndl) GetResource('DLOG', putDlgID);
  47.         if (dialog == NULL)                                /* No resource!            */
  48.             SetPt(&where, 80, 80);
  49.         else {
  50.             box = (**dialog).boundsRect;                /* Dialog shape            */
  51.             ReleaseResource((Handle) dialog);
  52.             where.h =
  53.                 (width(qd.thePort->portRect) - width(box)) / 2;
  54.             where.v = 
  55.                 ((height(qd.thePort->portRect) - GetMBarHeight()) / 3)
  56.                 + GetMBarHeight();
  57.         }
  58.         SFPutFile(where, promptString, defaultFileName, NULL, &reply);
  59.         if (reply.good == FALSE)
  60.             *fileRefNum = 0;
  61.         else {
  62.             FailOSErr(
  63.                 CreateOutputFile(creator, reply.fName, reply.vRefNum, fileRefNum),
  64.                 kErrCreateOutputFile
  65.             );
  66.             pstrcpy(fileName, reply.fName);
  67.             *volumeRefNum = reply.vRefNum;
  68.         }
  69. }
  70.  
  71. /*
  72.  * Create an output file. Return noErr on success. On success, fileRefNum has the
  73.  * refNum. On Failure, it has zero.
  74.  */
  75. OSErr
  76. CreateOutputFile(
  77.         OSType                            creator,
  78.         ConstStr255Param                fileName,
  79.         short                            vRefNum,
  80.         short                            *fileRefNum
  81.     )
  82. {
  83.         OSErr                            status;
  84.         
  85.         /*
  86.          * Create the file, elmininating any duplicate.
  87.          */
  88.         SetCursor(*GetCursor(watchCursor));
  89.         status = Create(fileName, vRefNum, creator, 'TEXT');
  90.         if (status == dupFNErr)     {                    /* Exists already?        */
  91.             status = FSDelete(fileName, vRefNum);
  92.             if (status == noErr)
  93.                 status = Create(fileName, vRefNum, creator, 'TEXT');
  94.         }
  95.         if (status == noErr)
  96.             status = FSOpen(fileName, vRefNum, fileRefNum);
  97.         if (status != noErr)
  98.             *fileRefNum = 0;
  99.         InitCursor();
  100.         return (status);
  101. }
  102.  
  103. /*
  104.  * Write one line of text to the output file. Fail on errors.
  105.  */
  106. void
  107. WriteAuditOutputLine(
  108.         ConstStr255Param                theText,
  109.         short                            fileRefNum
  110.     )
  111. {
  112.         long                            textLength;
  113.         static char                        gEndOfLine[1] = { kEndOfLine };
  114.  
  115.         textLength = theText[0];
  116.         if (textLength > 0) {
  117.             FailOSErr(
  118.                 FSWrite(fileRefNum, &textLength, &theText[1]),
  119.                 kErrWriteOutputFile
  120.             );
  121.         }
  122.         textLength = 1;
  123.         FailOSErr(
  124.             FSWrite(fileRefNum, &textLength, gEndOfLine),
  125.             kErrWriteOutputFile
  126.         );
  127. }
  128.  
  129. /*
  130.  * Close the Audit output file. If status == noErr, the file is closed normally,
  131.  * Otherwise, the program cleans up after an error. Errors return via the Failure
  132.  * mechanism. (Note: this includes a call with status != noErr.) The I/O model is
  133.  * as follows:
  134.  *        TRY {
  135.  *            PromptAndCreateAuditOutputFile(...);
  136.  *            WriteAuditOutputLine(...);
  137.  *            CloseAuditOutputFile(noErr, ...);
  138.  *        }
  139.  *        CATCH {
  140.  *            CloseAuditOutputFile(STATUS, ...);
  141.  *        }
  142.  *        ENDTRY;
  143.  */
  144. void
  145. CloseAuditOutputFile(
  146.         OSErr                            status,
  147.         short                            fileRefNum,
  148.         short                            volumeRefNum,
  149.         ConstStr255Param                fileName
  150.     )
  151. {
  152.         if (status == noErr) {
  153.             TRY {
  154.                 FailOSErr(FSClose(fileRefNum), kNoMessage);
  155.                 FailOSErr(FlushVol(NULL, volumeRefNum), kNoMessage);
  156.             }
  157.             CATCH {
  158.                 status = STATUS;
  159.                 NO_PROPAGATE;
  160.             }
  161.             ENDTRY;
  162.         }
  163.         if (status != noErr)
  164.             (void) FSDelete(fileName, volumeRefNum);
  165.         FailOSErr(status, kErrCloseOutputFile);
  166. }
  167.